package main

import (
	"github.com/streadway/amqp"
	"log"
	"os"
	"strings"
)

func main() {
	connection, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
	}
	defer connection.Close()
	channel, err := connection.Channel()
	defer channel.Close()

	channel.ExchangeDeclare("logs_topic", "topic", true, false, false, false, nil)
	body := bodyFrom(os.Args)
	channel.Publish("logs_topic", severityFrom(os.Args), false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte(body)})
	log.Printf(" [x] Sent %s", body)
}
func bodyFrom(args []string) string {
	var s string
	if (len(args) < 3) || os.Args[2] == "" {
		s = "hello"
	} else {
		s = strings.Join(args[2:], " ")
	}
	return s
}

func severityFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "anonymous.info"
	} else {
		s = os.Args[1]
	}
	return s
}
